home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-07 | 5.2 KB | 216 lines |
- /*
- PC Plus sample Java app.
- Demonstrates the definition of user-defined Classes.
- */
-
- import java.awt.*;
- import java.util.*;
-
- public class Frame1 extends Frame {
-
- Vector v = new Vector();
-
- void button1_Clicked(Event event) {
- // v is a Vector of mixed Objects.
- // This method checks the Object types using 'instanceof'
- // Then it prints the results into a List box.
-
- list1.clear();
- for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
- Thing t = (Thing)e.nextElement();
- list1.addItem(t.getName());
- list1.addItem(t.getDescription());
- if (t instanceof Object)
- list1.addItem("[This is an Object]");
- if (t instanceof Thing)
- list1.addItem("[This is a Thing]");
- if (t instanceof Creature)
- list1.addItem("[This is a Creature] species = " + ((Creature) t).getSpecies());
- if (t instanceof Treasure)
- list1.addItem("[This is a Treasure] value = " + ((Treasure) t).getValue());
- list1.addItem( "--------------------" );
-
- }
-
-
- }
-
- void Open_Action(Event event) {
- OpenFileDialog.show();
- }
-
- void About_Action(Event event) {
- (new AboutDialog(this, "About...", false)).show();
- }
-
- void Exit_Action(Event event) {
- (new QuitDialog(this, "Quit the Application?", false)).show();
- }
-
- public Frame1() {
-
- //{{INIT_CONTROLS
- setLayout(null);
- addNotify();
- resize(insets().left + insets().right + 431,insets().top + insets().bottom + 464);
- OpenFileDialog = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
- list1 = new java.awt.List(0,false);
- add(list1);
- list1.reshape(insets().left + 7,insets().top + 8,416,345);
- button1 = new java.awt.Button("Display My Objects!");
- button1.reshape(insets().left + 133,insets().top + 383,159,39);
- add(button1);
- setTitle("A Basic Application");
- //}}
-
- //{{INIT_MENUS
- mainMenuBar = new java.awt.MenuBar();
-
- menu1 = new java.awt.Menu("File");
- menu1.add("Open...");
- menu1.add("Save");
- menu1.add("Save As...");
- menu1.addSeparator();
- menu1.add("Exit");
- mainMenuBar.add(menu1);
-
- menu2 = new java.awt.Menu("Edit");
- menu2.add("Cut");
- menu2.add("Copy");
- menu2.add("Paste");
- mainMenuBar.add(menu2);
-
- menu3 = new java.awt.Menu("Help");
- menu3.add("About");
- mainMenuBar.add(menu3);
- setMenuBar(mainMenuBar);
- //}}
- }
-
- public Frame1(String title) {
- this();
- setTitle(title);
- }
-
- public synchronized void show() {
- move(50, 50);
- super.show();
-
- v.addElement( new Thing("A Wotsit", "An ordinary Thing"));
- v.addElement( new Creature("A Troll", "A nasty fellow", "Trollus trollidus" ));
- v.addElement( new Treasure("A Sword", "A jewel-encrusted weapon", 550));
-
- }
-
- public boolean handleEvent(Event event) {
- if (event.id == Event.WINDOW_DESTROY) {
- hide(); // hide the Frame
- dispose();
- System.exit(0);
- return true;
- }
- if (event.target == button1 && event.id == Event.ACTION_EVENT) {
- button1_Clicked(event);
- }
- return super.handleEvent(event);
- }
-
- public boolean action(Event event, Object arg) {
- if (event.target instanceof MenuItem) {
- String label = (String) arg;
- if (label.equalsIgnoreCase("Open...")) {
- Open_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("About")) {
- About_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Exit")) {
- Exit_Action(event);
- return true;
- }
- }
- return super.action(event, arg);
- }
-
- static public void main(String args[]) {
- (new Frame1()).show();
- }
-
- //{{DECLARE_CONTROLS
- java.awt.FileDialog OpenFileDialog;
- java.awt.List list1;
- java.awt.Button button1;
- //}}
-
- //{{DECLARE_MENUS
- java.awt.MenuBar mainMenuBar;
- java.awt.Menu menu1;
- java.awt.Menu menu2;
- java.awt.Menu menu3;
- //}}
- }
-
-
- // ---- OUR OWN USER-DEFINED CLASS HIERARCHY ----
- // class Thing
- // any object that has a name and a description
- class Thing {
- private String name;
- private String description;
-
- Thing( String aName, String aDescription) {
- this.name = aName;
- this.description = aDescription;
- }
-
- String getName() {
- return name;
- }
-
- void setName(String aName) {
- this.name = aName;
- }
-
- String getDescription() {
- return description;
- }
-
- void setDescription(String aDescription) {
- this.description = aDescription;
- }
- } // END: class Thing
-
-
- // class Creature
- // a descendent of Thing which also has a 'species'
- class Creature extends Thing {
- private String species;
-
- Creature( String aName, String aDescription, String aSpecies ) {
- super( aName, aDescription );
- this.species = aSpecies;
- }
-
- String getSpecies() {
- return species;
- }
-
- }
-
- // class Treasure
- // a descendent of Thing which also has a 'value'
- class Treasure extends Thing {
- private int value;
-
- Treasure( String aName, String aDescription, int aValue ) {
- super( aName, aDescription );
- this.value = aValue;
- }
-
- int getValue() {
- return value;
- }
-
- }